package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiShop;
import cn.com.api.entity.ApiType;
import cn.com.api.mapper.ApiShopsMapper;
import cn.com.api.mapper.ApiT_productsMapper;
import cn.com.api.mapper.ApiTypeMapper;
import cn.com.api.mapper.SystemMapper;
import cn.com.api.service.ApiGoodsService;
import cn.com.api.service.ApiShopsService;
import cn.com.api.service.ApiTypeService;
import cn.com.api.service.StartOnLoadService;
/*import cn.com.web.service.GoodsService;*/
import cn.com.util.*;

@Service
public class ApiTypeServiceImpl implements ApiTypeService {

	@Autowired
	private ApiTypeMapper apiTypeMapper;
	@Autowired
	private ApiGoodsService apiGoodsService;
	/*
	 * @Autowired private GoodsService goodsService;
	 */
	@Autowired
	private ApiShopsService apiShopsService;
	@Autowired
	private ApiShopsMapper apiShopsMapper;
	@Autowired
	private SystemMapper systemMapper;
	@Autowired
	private ApiT_productsMapper apiT_productsMapper;
	@Autowired
	private StartOnLoadService startOnLoadService;

	@Override
	public List<ApiType> getApiTypeListPage(PageInfo p) {
		return apiTypeMapper.getApiTypeListPage(p);
	}

	@Override
	public List<Map<String, Object>> getGoodsType(Object cBelong) {
		// TODO Auto-generated method stub
		return apiTypeMapper.getGoodsType(cBelong);
	}

	@Override
	public List<Map<String, Object>> getAllGoodsTypes() {
		// TODO
		List<Map<String, Object>> first_list = new ArrayList<Map<String, Object>>();
		first_list = apiTypeMapper.getGoodsType(0);
		for (Map<String, Object> type : first_list) {
			// 获取二级分类
			List<Map<String, Object>> second_list = apiTypeMapper
					.getGoodsType(type.get("c_code"));
			for (Map<String, Object> second_type : second_list) {
				// 获取三级分类
				List<Map<String, Object>> third_list = apiTypeMapper
						.getGoodsType(second_type.get("c_code"));
				second_type.put("childs", third_list);
			}
			type.put("childs", second_list);
		}
		return first_list;
	}

	// 鏌ヨ浜岀骇鑿滃崟
	@Override
	public List<ApiType> gettwoType(@Param("c_id") Integer c_id) {
		// TODO Auto-generated method stub
		return apiTypeMapper.gettwoType(c_id);
	}

	// 鏌ヨ涓夌骇鑿滃崟
	@Override
	public List<ApiType> getthreeType(@Param("c_id") Integer c_id) {
		// TODO Auto-generated method stub
		return apiTypeMapper.getthreeType(c_id);
	}

	@Override
	public List<ApiType> getTypesByCBelong(Integer cBelong) {
		List<ApiType> list = apiTypeMapper.getTypesByCBelong(cBelong);
		List<ApiType> new_list = new ArrayList<ApiType>();

		if (list != null && list.size() > 0 && 0 == cBelong) {
			Map<String, String> menuMap = ViewUtils
					.transToMenuMap(startOnLoadService.dictionaryMap());
			// 排除非自营分类
			for (ApiType bean : list) {
				if (bean.getcIsziying() == 1) {
					new_list.add(bean);
				}
			}
			// 自营类循环
			for (ApiType bean : new_list) {// 濡傛灉鏄�2涓寜閽紝杩斿洖瑙嗗浘鏄剧ず鏍峰紡鏍囪瘑
				if (bean.getcIsziying() == 0) {
					list.remove(bean);
				}
				if ("View1".equals(menuMap.get(bean.getcCode() + ""))) {// 1-绗竴绫昏繑鍥濾iew1
					bean.setView("View1");
				} else if ("View2".equals(menuMap.get(bean.getcCode() + ""))) {// 瀹舵斂鏈嶅姟
					bean.setView("View2");
				} else if ("View3".equals(menuMap.get(bean.getcCode() + ""))) {// 澶栧崠
					bean.setView("View3");
				} else if ("View4".equals(menuMap.get(bean.getcCode() + ""))) {// 椴滆姳铔嬬硶
					bean.setView("View4");
				} else if ("View5".equals(menuMap.get(bean.getcCode() + ""))) {// 鍖荤枟淇濆仴-鏁欒偛
					bean.setView("View5");
				}
			}
		}
		return new_list;
	}

	@Override
	public void searchTypeGoods(Map<String, Object> map,
			Map<String, String> params) {
		Integer cCode = Integer.parseInt(params.get("cCode"));
		Integer city_id = Integer.parseInt(params.get("city_id"));
		Integer pageCount = Integer.parseInt(params.get("pageCount"));
		PageInfo page = new PageInfo();
		page.setCurrentPage(pageCount);
		Map<String, String> menuMap = ViewUtils
				.transToMenuMap(startOnLoadService.dictionaryMap());
		if (menuMap.containsKey(cCode + "")) {
			// 绗簩绾ц彍鍗�绗笁绾ц彍鍗�+榛樿鍟嗗搧鍒楄〃
			List<ApiType> typeList = apiTypeMapper.getTypesByCBelong(cCode);// 浜岀骇鑿滃崟
			List<Map<String, Object>> goodsList = null;

			if (typeList != null && typeList.size() > 0) {
				if ("View1".equals(menuMap.get(cCode + ""))) {// 1-绗竴绫昏繑鍥濾iew1
					// 璧嬪�涓夌骇鑿滃崟
					for (ApiType type : typeList) {
						List<ApiType> list = apiTypeMapper
								.getTypesByCBelong(type.getcCode());
						type.setListType(list);
					}
					int type = typeList.get(0).getcCode();// 绗簩绾ц彍鍗曠涓�釜鐨勯粯璁ゅ晢鍝佸垪琛�
					List<Integer> types = getGoodTypesByParentType(type);
					// goodsList =
					// apiGoodsService.serchGoodsByType(type,city_id);
					String typeid = "";
					for (Integer l : types) {
						typeid += l + ",";
					}
					String[] goodstype = typeid.split(",");
					/*
					 * goodsList = goodsService.searchGoods(goodstype,
					 * Integer.valueOf(city_id), 0, page);
					 */
					map.put("View", "View1");
					map.put("goodsList", goodsList);
				} else if ("View2".equals(menuMap.get(cCode + ""))) {// 瀹舵斂鏈嶅姟
					map.put("View", "View2");
				} else if ("View3".equals(menuMap.get(cCode + ""))) {// 澶栧崠
					// 澶栧崠杩斿洖鍟嗛摵鍒楄〃
					int type = typeList.get(0).getcCode();
					List<ApiShop> shopList = apiShopsMapper
							.searchShopsByTypeListPage(type, city_id, page);
					map.put("View", "View3");
					map.put("shopList", shopList);
				}
			}
			map.put("typeList", typeList);// 鑿滃崟鏁版嵁
			// map.put("goodsList", goodsList);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "successs");
		}
	}

	// 鏍规嵁id鑾峰彇
	public ApiType findTypeById(Integer c_code) {
		ApiType type = apiTypeMapper.findTypeById(c_code);
		return type;
	}

	// 娣诲姞
	public void insertType(ApiType type) {
		// 鏍规嵁shop_id 鏌ヨ搴楅摵鎵�睘鐨勫垎绫�
		// 根据shop_id 查询所属分类
		ApiShop shop = apiShopsMapper.findT_shopsById(type.getcSid());
		if (shop != null) {
			Integer shopType = shop.getcType();
			Integer shopParentType = apiTypeMapper
					.getParentTypeByType(shopType);

			type.setcGrade(shopParentType);
			type.setcBelong(shopType);
		}
		apiTypeMapper.insertType(type);
		return;
	}

	public void insertType4erp(ApiType type) {
		apiTypeMapper.insertType4erp(type);
		return;
	}

	// 淇敼
	public void updateType(ApiType type) {
		apiTypeMapper.updateType(type);
		return;
	}

	// 鍒犻櫎
	public int deleteType(Integer c_code) {
		int ret_I = apiTypeMapper.deleteType(c_code);
		return ret_I;
	}

	// 鏌ヨ鍟嗗鐨勫晢鍝佸垎绫�
	public List<ApiType> getTypesByShopId(Integer c_sid) {
		List<ApiType> types = new ArrayList<ApiType>();

		Integer shop_type = apiShopsService.searType(c_sid + "");
		int flag = checkIsFoodTakeAway(shop_type);
		if (flag == 2) {// 椴滆姳铔嬬硶銆佺敓娲绘湇鍔�
			ApiType type = apiTypeMapper.findTypeById(shop_type);
			types.add(type);
		} else if (flag == 1) {// 缇庨澶栧崠
			types = apiTypeMapper.getTypesByShopId(c_sid);
		} else {// 鍖昏嵂闂ㄥ簵銆佹暀鑲插煿璁�鐢熸椿鏈嶅姟
			types = apiTypeMapper.bybelong(shop_type);
		}
		return types;
	}

	// 鏍规嵁鍟嗗搧绫诲瀷鑾峰彇鍟嗗搧鎬绘暟
	public int getGoodsCountByTypeId(Integer type_id) {
		int ret_i = apiTypeMapper.getGoodsCountByTypeId(type_id);
		return ret_i;
	}

	@Override
	// 鏍规嵁鍟嗗搧绫诲埆鑾峰彇 锛堝綋鍓嶇被鍒�瀛愮被锛�
	public List<Integer> getGoodTypesByParentType(int goods_type) {

		// 鑾峰彇瀛愮被
		List<Integer> child_type_code_list = new ArrayList<Integer>();
		// 鍔犲叆鑷繁
		child_type_code_list.add(goods_type);

		List<ApiType> types = apiTypeMapper.getTypesByCBelong(goods_type);
		for (ApiType type : types) {
			Integer type_code = type.getcCode();
			child_type_code_list.add(type_code);
		}

		return child_type_code_list;
	}

	@Override
	public List<ApiType> findTypes() {
		// TODO
		List<ApiType> types = apiTypeMapper.findTypes();
		return types;
	}

	// 鏍圭骇鍒嗙被 -浼�
	@Override
	public List<ApiType> getTypesByCGrade(Integer cGrade) {
		List<ApiType> types = apiTypeMapper.getTypesByCGrade(cGrade);
		return types;
	}

	@Override
	public List<ApiType> getSecondTypes() {
		// TODO
		List<ApiType> types = apiTypeMapper.getSecondTypes();
		return types;
	}

	// 鑾峰彇绯荤粺涓夌骇鍒嗙被
	public List<ApiType> getThirdTypes() {
		// TODO
		List<ApiType> types = apiTypeMapper.getThirdTypes();
		return types;
	}

	@Override
	public List<ApiType> getFirstTypes() {
		//
		List<ApiType> types = apiTypeMapper.getFirstTypes();
		return types;
	}

	@Override
	public List<ApiType> findTypesByCmod(String c_mod) {
		//
		List<ApiType> types = apiTypeMapper.findTypesByCmod(c_mod);
		return types;
	}

	@Override
	public List<ApiType> findTypes4Shops() {
		//
		List<ApiType> types = apiTypeMapper.findTypes4Shops();
		return types;
	}

	@Override
	// 鏍规嵁belong鏌ヨ瀵瑰簲code
	public List<ApiType> bybelong(Integer c_belong) {

		return apiTypeMapper.bybelong(c_belong);
	}

	// 判断店铺是否是美食外卖 1-美食外卖 2-生活服务 3-其他 0-自营
	@Override
	public int checkIsFoodTakeAway(Integer shopType) {
		int flag = 0; // 判断是否为鲜花蛋糕 或者 外卖店

		try {
			int type = apiTypeMapper.getParentTypeByType(shopType);
			Map<String, String> menuMap = startOnLoadService.dictionaryMap();

			if (menuMap.get("MENU_MEISHI").equals(type + "")) {
				flag = 1;// 美食
			} else if (menuMap.get("MENU_XIANHUADANGAO").equals(type + "")
					|| menuMap.get("MENU_FUWU").equals(type + "")) { // 鐢熸椿鏈嶅姟
				flag = 2; // 鲜花蛋糕
			} else {
				flag = 0;
			}
		} catch (Exception e) {

		}
		return flag;
	}

	// 判断店铺类型 1-美食外卖 2-生活服务 3-其他
	@Override
	public int getShopType(Integer shopType) {
		int flag = 3;
		if (-1 == shopType) {
			flag = 0;// 自营店
		} else {
			int type = apiTypeMapper.getParentTypeByType(shopType);
			Map<String, String> menuMap = startOnLoadService.dictionaryMap();
			if (menuMap.get("MENU_MEISHI").equals(type + "")) {
				flag = 1;// 美食外卖
			} else if (menuMap.get("MENU_FUWU").equals(type + "")) {
				flag = 2;// 生活服务
			} else {
				flag = 3;// 其他
			}
		}
		return flag;
	}

	// 鑾峰彇鑷惀涓�骇鍝佺被
	public List<ApiType> getBigTypesZiYing() {
		return apiTypeMapper.getBigTypesZiYing();
	}

	/**
	 * 鑾峰彇闈炶嚜钀ヤ竴绾у搧绫�
	 */
	@Override
	public List<ApiType> getBigTypes() {
		return apiTypeMapper.getBigTypes();
	}

	@Override
	public List<ApiType> getShopTypes(int c_sid) {
		return apiTypeMapper.getTypesByShopId(c_sid);
	}

	@Override
	// 鏍规嵁cName鏌ユ壘闈炶嚜钀�
	public List<ApiType> bycnamefzyListPage(Map<String, Object> conditions,
			PageInfo pageInfo) {
		return apiTypeMapper.bycnamefzyListPage(conditions, pageInfo);
	}

	@Override
	// 鏍规嵁cName鏌ユ壘鑷惀
	public List<ApiType> bycnamezyListPage(Map<String, Object> conditions,
			PageInfo pageInfo) {
		return apiTypeMapper.bycnamezyListPage(conditions, pageInfo);
	}

	@Override
	// 鑾峰彇闈炶嚜钀ュ搧绫�
	public List<ApiType> getTypes() {
		return apiTypeMapper.getTypes();
	}

	@Override
	// 鑾峰彇鑷惀鍝佺被
	public List<ApiType> getTypesZiYing() {
		return apiTypeMapper.getTypesZiYing();
	}

	@Override
	public List<ApiType> getSecondTyes(boolean isZiYing) {
		return apiTypeMapper.getLevelTypes(isZiYing, 2);
	}

	@Override
	public List<ApiType> getThirdTypes(boolean isZiYing) {
		return apiTypeMapper.getLevelTypes(isZiYing, 3);
	}

	@Override
	public List<Integer> getNoZiYingSubTypes(int parentTypeId) {
		return apiTypeMapper.getSubTypeIds(parentTypeId, false);
	}

	@Override
	public List<Integer> getZiYingSubTypes(int parentTypeId) {
		return apiTypeMapper.getSubTypeIds(parentTypeId, true);
	}

	@Override
	// 闈炶嚜钀ュ垎椤�
	public List<ApiType> getfeizyListPage(PageInfo pageInfo) {

		return apiTypeMapper.getfeizyListPage(pageInfo);
	}

	@Override
	// 鑷惀鍒嗛〉
	public List<ApiType> getzyListPage(PageInfo pageInfo) {

		return apiTypeMapper.getzyListPage(pageInfo);
	}

	@Override
	public Map<String, Object> searShengXian() {
		return apiTypeMapper.searShengXian();
	}

	@Override
	public ApiType getMeiShiTypeByShopId(String shop_id) {
		ApiType type = null;
		Integer shopType = apiShopsService.searType(shop_id);
		if (checkIsFoodTakeAway(shopType) == 1) {
			// 判断是否有销量大于10的商品--在售中的商品
			Integer count = apiT_productsMapper.findProductsCountBySold(
					shop_id, 10);
			if (count != null && count > 0) {
				type = new ApiType();
				type.setcCode(-Integer.valueOf(shop_id));// 店铺id加上-
				type.setcName("热销榜");
			}
		}
		return type;
	}

	public List<ApiType> getTypesByParent(Integer parentid) throws Exception {
		List<ApiType> list = apiTypeMapper.bybelong(parentid);
		for (ApiType apitype : list) {
			List<ApiType> bybelong = apiTypeMapper.byParent(apitype.getcCode());
			if (parentid == 0) {// 查询所以类型
				for (ApiType apitype2 : bybelong) {
					List<ApiType> bybelong2 = apiTypeMapper.byParent(apitype2
							.getcCode());
					apitype2.setListType(bybelong2);
				}
			}
			apitype.setListType(bybelong);
		}
		return list;
	}
}
